package cn.lystudio.rohmtimeopwide_background_v11.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.SSD2MoldRecordPO;
import cn.lystudio.rohmtimeopwide_background_v11.mapper.SSD2MoldRecordMapper;
import cn.lystudio.rohmtimeopwide_background_v11.service.SSD2MoldRecordService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
@DS("master")
public class SSD2MoldRecordServiceImpl implements SSD2MoldRecordService {
	@Resource
	private SSD2MoldRecordMapper ssd2MoldRecordMapper;
	@Override
	public void addJxRecord(SSD2MoldRecordPO ssd2MoldRecordPO) {
		ssd2MoldRecordMapper.insert(ssd2MoldRecordPO);
	}

	@Override
	public List<SSD2MoldRecordPO> getJxRecordTimeLine(String machineNo, String beginDate, String endDate) {
		LambdaQueryWrapper<SSD2MoldRecordPO> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(SSD2MoldRecordPO::getMachineNo,machineNo)
				.gt(SSD2MoldRecordPO::getRecordTimeBeginDT,beginDate)
				.le(SSD2MoldRecordPO::getRecordTimeBeginDT,endDate);
		return ssd2MoldRecordMapper.selectList(wrapper);
	}

	@Override
	public SSD2MoldRecordPO getJxRecordByID(int recordID) {
		return ssd2MoldRecordMapper.selectById(recordID);
	}

	@Override
	public void uptJxRecord(SSD2MoldRecordPO ssd2MoldRecordPO) {
		ssd2MoldRecordMapper.updateById(ssd2MoldRecordPO);
	}

	@Override
	public void delJxRecord(Integer recordID) {
		ssd2MoldRecordMapper.deleteById(recordID);
	}

	@Override
	public List<SSD2MoldRecordPO> getSSD2MoldRecordList(String machineNo, String selectedDate) {
		String beginDate = selectedDate + " 08:00:00";
		String endDate = DateUtil.format(DateUtil.offsetDay(DateUtil.parse(selectedDate),1),"yyyy-MM-dd") + " 08:00:00";
		LambdaQueryWrapper<SSD2MoldRecordPO> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(SSD2MoldRecordPO::getMachineNo,machineNo)
				.ge(SSD2MoldRecordPO::getRecordTimeBeginDT,beginDate)
				.lt(SSD2MoldRecordPO::getRecordTimeBeginDT,endDate)
				.orderByAsc(SSD2MoldRecordPO::getRecordTimeBeginDT);
		return   ssd2MoldRecordMapper.selectList(wrapper);
	}

}
